#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

create_dir()
{
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p $path; else path=$path`date "+%y%m%d%H%M%S"` && mkdir -p $path; fi
    echo $path
}

buildpath=$(create_dir $1)
installpath=$(create_dir $2)

yum install -y perl-JSON perl-File-Which perl-Devel-Size perl-LWP-Protocol-https sudo tar wget python3 bc libtool numactl systemd-devel valgrind flex patch mysql environment-modules zlib-devel
pip3 install h5py -i https://mirrors.aliyun.com/pypi/simple/
source /etc/profile

set -e
dep_dir="dependency"
bash $dep_dir/bisheng-compiler-2.1.0.sh $buildpath $installpath
module use $installpath/bisheng-compiler-2.1.0
module load $installpath/bisheng-compiler-2.1.0/bisheng_modulefiles
bash $dep_dir/rmblast-2.10.0.sh $buildpath $installpath
module use $installpath/rmblast
module load $installpath/rmblast/rmblast_modulefiles
#bisheng
bash $dep_dir/hmmer-3.3.2.sh $buildpath $installpath
module use $installpath/hmmer
module load $installpath/hmmer/hmmer_modulefiles
#use sse2neon
bash $dep_dir/trf-4.09.1.sh $buildpath $installpath
module use $installpath/trf
module load $installpath/trf/trf_modulefiles
#bisheng
bash $dep_dir/RepeatScout-1.0.6.sh $buildpath $installpath
module use $installpath/RepeatScout
module load $installpath/RepeatScout/RepeatScout_modulefiles
#bisheng
bash $dep_dir/RECON-1.08.sh $buildpath $installpath
module use $installpath/RECON
module load $installpath/RECON/RECON_modulefiles
#bisheng
bash $dep_dir/cd-hit-4.8.1.sh $buildpath $installpath
module use $installpath/cd-hit
module load $installpath/cd-hit/cd-hit_modulefiles
#bisheng
bash $dep_dir/genometools-1.6.0.sh $buildpath $installpath
module use $installpath/genometools
module load $installpath/genometools/genometools_modulefiles
#bisheng
bash $dep_dir/mafft-7.471.sh $buildpath $installpath
module use $installpath/mafft
module load $installpath/mafft/mafft_modulefiles
#bisheng
bash $dep_dir/NINJA-0.97.sh $buildpath $installpath
module use $installpath/NINJA
module load $installpath/NINJA/NINJA_modulefiles
#bisheng sse2neon
bash $dep_dir/ucsc_tools.sh $buildpath $installpath
module use $installpath/ucsc_tools
module load $installpath/ucsc_tools/ucsc_tools_modulefiles
#bisheng
bash $dep_dir/RepeatMasker-4.1.2.sh $buildpath $installpath
module use $installpath/RepeatMasker
module load $installpath/RepeatMasker/RepeatMasker_modulefiles
#use sse2neon
bash $dep_dir/coseg-0.2.2.sh $buildpath $installpath
module use $installpath/coseg
module load $installpath/coseg/coseg_modulefiles
#bisheng
bash $dep_dir/LTR_retriever-2.9.0.sh $buildpath $installpath
module use $installpath/LTR_retriever
module load $installpath/LTR_retriever/LTR_retriever_modulefiles
#binary file(do not need to be compiled)
bash RepeatModeler-2.0.3.sh $buildpath $installpath << EOF


y
EOF


mkdir -p pkg/RepeatModeler-2.0.3-hpc && cp -r $installpath/* pkg/RepeatModeler-2.0.3-hpc/
cat > "pkg/RepeatModeler-2.0.3-hpc/run.sh" <<EOF
yum install -y perl-JSON perl-File-Which perl-Devel-Size perl-LWP-Protocol-https sudo tar wget python3 bc libtool numactl systemd-devel valgrind flex patch lmdb* libpng libpng-devel libuuid-devel mysql environment-modules
sudo yum install -y zlib-devel && pip3 install h5py

pwd_path=\$('pwd')
source /etc/profile &> /dev/null

current_dir=\$(cd \$(dirname "\${BASH_SOURCE[0]}") && pwd)
for d in \$(ls \$current_dir)
do
    [[ ! -f "\$current_dir/\$d" ]] && cd \$current_dir/\$d && module use \$(pwd) && module load \$(pwd)/*modulefiles
done
sed -i "s#use lib \".*/RepeatMasker\";#use lib \"\$RepeatMasker\";#" \$coseg/preprocessAlignments.pl
sed -i \
    -e "s#BLAST+=.*#BLAST+=\$rmblast/bin#" \
    -e "s#RepeatMasker=.*#RepeatMasker=\$RepeatMasker#" \
    -e "s#HMMER=.*#HMMER=\$hmmer/bin#" \
    -e "s#CDHIT=.*#CDHIT=\$cd_hit#" \
    \$LTR_retriever/paths
sed -i "s#^\$path =.*/RECON/bin#\$path = \"\$RECON/bin\";#" \$RECON/scripts/recon.pl
cd \$RepeatMasker && perl configure \
	 -hmmer_dir=\$hmmer/bin \
	 -rmblast_dir=\$rmblast/bin \
	 -libdir=\$RepeatMasker/Libraries \
	 -trf_prgm=\$trf/trf \
	 -default_search_engine=rmblast
cd \$RepeatModeler && echo -e "\n \n \n" | perl configure \
	-cdhit_dir=\$cd_hit -genometools_dir=\$genometools/bin \
	-ltr_retriever_dir=\$LTR_retriever -mafft_dir=\$mafft/bin \
	-ninja_dir=\$NINJA/NINJA -recon_dir=\$RECON/bin \
	-repeatmasker_dir=\$RepeatMasker \
	-rmblast_dir=\$rmblast/bin -rscout_dir=\$RepeatScout \
	-trf_dir=\$trf \
	-ucsctools_dir=\$ucsc_tools
source /etc/profile &> /dev/null
cd \$pwd_path
EOF
cd pkg/ && tar zcvf RepeatModeler-2.0.3-hpc.tar.gz RepeatModeler-2.0.3-hpc

rm -rf $buildpath $installpath
